package 博客管理系统;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.*;
import java.awt.HeadlessException;

public class Login extends JFrame implements ActionListener{
    private static final Statement PraparedStatement = null;
    Connection con;

    JLabel id = new JLabel("身份");
    String[] str={"用户","管理员"};
    JComboBox jcb=new JComboBox(str);
    JLabel useraccont = new JLabel("账号");
    JLabel userpassword = new JLabel("密码");
    JTextField id1 = new JTextField(10);
    JTextField useraccont1 = new JTextField(10);
    JTextField userpassword1 = new JTextField(10);
    JButton yes = new JButton("确认");
    JButton no  = new JButton("取消");

    public Login() {
        setTitle("博客管理系统");

        setLayout(null);

        id.setBounds(100,90,100,50);
        jcb.setBounds(150,90,100,50);
        id1.setBounds(300,100,200,30);
        useraccont.setBounds(100,140,100,50);
        useraccont1.setBounds(300,150,200,30);
        userpassword.setBounds(100,190,100,50);
        userpassword1.setBounds(300,200,200,30);


        yes.setBounds(100,450, 100, 60);
        no.setBounds(400, 450, 100,60);
        add(id);
        add(id1);
        add(useraccont);
        add(useraccont1);
        add(userpassword);
        add(userpassword1);
        add(jcb);

        add(yes);
        add(no);

        yes.addActionListener(this);
        no.addActionListener(this);
        jcb.addActionListener(this);
        this.add(jcb);

        setVisible(true);
        setBounds(600,300,600,600);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
    public static void main(String args[]){
        new Login();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource()==yes) {                      //如果你点击的是确认按钮
            String s1=id1.getText();                    //获取文本框中的信息
            String s2=useraccont1.getText();
            String s3=userpassword1.getText();


            //加载数据库驱动
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            }
            catch(Exception e1) {

            }
            Connection con;
            Statement st;
            ResultSet rs;
            //连接数据库
            String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=BlogSystemLD";
            String userName="sa";
            String userPwd="123";                                  //自己的数据库密码
            String sql="select * from UserInfo";

            try {
                con = DriverManager.getConnection(dbURL, userName, userPwd);
                st = con.createStatement();
                rs = st.executeQuery(sql);
                int q=0;
                while (rs.next()) {
                    q++;
                }

                if(q>0){
                    JOptionPane.showMessageDialog(this, "登陆成功！","消息对话框",JOptionPane.WARNING_MESSAGE);
                    this.dispose();
                    new CommFrame();
                }
                else{
                    JOptionPane.showMessageDialog(this, "账号或者密码错误!","消息对话框",JOptionPane.WARNING_MESSAGE);
            }


            } catch (SQLException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }


            JOptionPane.showMessageDialog(null, "数据库连接成功", "连接数据库",
                    JOptionPane.INFORMATION_MESSAGE);



            try {
                if(init()==true) {                                                        //假如添加的id数据库中已经存在，则不添加
                    JOptionPane.showMessageDialog(null, "数据库中此 Id 已存在，不可添加", "提示",
                            JOptionPane.INFORMATION_MESSAGE);
                    dispose();
                }
                else if(init()==false){                                        //如果没有，则执行添加
                   sql="insert into User(id,useraccont,userpassword)"+"values('"+s1+"','"+s2+"','"+s3+"')";                                                                                        //创建sql语句

                    Statement statement = null;

                    try {
                        con = DriverManager.getConnection(dbURL, userName, userPwd);
                        statement = con.createStatement();
                    } catch (SQLException e2) {
                        e2.printStackTrace();
                    }
                    try {
                        int result = statement.executeUpdate(sql);                                //执行sql语句
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }

                    JOptionPane.showMessageDialog(null, "添加用户信息成功", "完成",
                            JOptionPane.INFORMATION_MESSAGE);
                    dispose();

                }
            } catch (HeadlessException | SQLException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }

        }
        else if(e.getSource()==no) {
            dispose();
        }
    }

    //判断添加的Id在数据库中是否存在的方法
    public boolean init() throws SQLException {

        String sql1="select Id FROM Students where Id='"+id+"'";

        PreparedStatement pstm = con.prepareStatement(sql1);// 执行查询

        ResultSet rs = pstm.executeQuery();

        boolean result = false;

        while(rs.next()) {

            result = true;
        }
        rs = pstm.executeQuery();
        return result;
    }
}
